package demo1;

public class HashBuck<K,V> {
    static class Node<K,V> {
        public K key;
        public V val;
        public Node<K,V> next;

        public Node(K key, V val) {
            this.key = key;
            this.val = val;
        }
    }
    public Node<K,V>[] array;
    public int usedSize;

    public HashBuck() {
        this.array = (Node<K,V>[])new Node[10];
    }

    public static final double LOAD_FACTOR = 0.75;

    public void put(K key, V val) {
        int index = key.hashCode() % array.length;
        Node<K,V> cur = array[index];
        while (cur != null) {
            if (cur.key.equals(key)) {
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        Node<K,V> newNode = new Node<K,V>(key,val);
        newNode.next = array[index];
        array[index] = newNode;
        usedSize++;
        if (getLOAD_FACTOR() >= LOAD_FACTOR) {
            //扩容
            resize();
        }
    }
    //扩容
    private void resize() {

    }

    public double getLOAD_FACTOR() {
        return usedSize * 1.0 / array.length;
    }

    public V get(K key) {
        int index = key.hashCode() % array.length;
        Node<K,V> cur = array[index];
        while (cur != null) {
            if (cur.key.equals(key)) {
                return cur.val;
            }
            cur = cur.next;
        }
        return null;
    }
}
